package com.bestcem.xm.txtanls.controller.param.analysis;

import com.bestcem.xm.common.core.constant.RegTypes;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.txtanls.grpc.service.param.TextFieldParam;
import com.bestcem.xm.txtanls.grpc.service.param.analysis.CreateAnalysisProjectParam;
import com.bestcem.xm.txtanls.service.dto.analysis.AnalysisProjectDTO;
import com.bestcem.xm.txtanls.service.impl.SurveyServiceImpl;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 创建文本分析项目入参校验
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v2.0
 * @date 2021/5/10-15:21
 */
@ApiModel("创建文本分析项目入参")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class AnalysisProjectParam {

    /**
     * 项目名称
     */
    @ApiModelProperty(value = "项目名称;  15位的名字, 不能包含下面任何字符: \\/:*?\"<>|", required = true)
    @JsonProperty(value = "name")
    @NotBlank(message = ErrorMessage.NAME_NULL_ERROR)
    private String name;

    /**
     * 数据表id
     */
    @ApiModelProperty(value = "数据表id;  24位的Objectid", required = true)
    @JsonProperty(value = "table_id")
    @NotBlank(message = ErrorMessage.TABLE_ID_NULL_ERROR)
    private String tableId;

    /**
     * 数据表名称
     */
    @ApiModelProperty(value = "数据表名称;  数据表id对应表名称", required = true)
    @JsonProperty(value = "table_name")
    @NotBlank(message = ErrorMessage.TABLE_NAME_NULL_ERROR)
    private String tableName;

    /**
     * 训练字段列表
     */
    @ApiModelProperty(value = "训练字段列表;  训练字段列表", required = true)
    @JsonProperty(value = "textfieldList")
    @Valid
    @NotEmpty(message = ErrorMessage.TEXT_FIELD_NULL_ERROR)
    private List<FieldParam> textFieldList;

    /**
     * 时间字段id
     */
    @ApiModelProperty(value = "时间字段id;  24位的Objectid", required = true)
    @JsonProperty(value = "timefield_id")
    @NotBlank(message = ErrorMessage.TIME_FIELD_ID_NULL_ERROR)
    private String timeFieldId;

    /**
     * 时间字段名称
     */
    @ApiModelProperty(value = "时间字段名称;  时间字段id对应字段名称", required = true)
    @JsonProperty(value = "timefield_name")
    @NotBlank(message = ErrorMessage.TIME_FIELD_NAME_NULL_ERROR)
    private String timeFieldName;

    /**
     * 训练模型字段
     */
    @JsonProperty(value = "model_id")
    @NotBlank(message = "训练模型不能为空")
    private String modelId;

    public static class ErrorMessage {
        public static final String NAME_NULL_ERROR = "项目名称不能为空";
        public static final String NAME_SIZE_ERROR = "项目名称不能超过" + SurveyServiceImpl.NAME_LENGTH + "个字";
        public static final String NAME_FORMAT_ERROR = "项目名称非法，不能包含下面任何字符: \\/:*?\"<>|";
        public static final String TABLE_ID_NULL_ERROR = "数据表不能为空";
        public static final String TABLE_NAME_NULL_ERROR = "数据表名称不能为空";
        public static final String TEXT_FIELD_NULL_ERROR = "训练字段列表不能为空";
        public static final String TIME_FIELD_ID_NULL_ERROR = "时间字段不能为空";
        public static final String TIME_FIELD_NAME_NULL_ERROR = "时间字段名称不能为空";
    }

    public static XmResultJson<AnalysisProjectDTO> validateParam(AnalysisProjectParam param) {
        if (param.getName().length() > 15) {
            return XmResultJson.fail(5, ErrorMessage.NAME_SIZE_ERROR);
        }
        if (!Pattern.matches(RegTypes.NAME, param.getName())) {
            return XmResultJson.fail(5, ErrorMessage.NAME_FORMAT_ERROR);
        }
        if (!Pattern.matches(RegTypes.OBJECT_ID, param.getTableId())) {
            return XmResultJson.fail(5, "数据表id格式错误");
        }
        if (!Pattern.matches(RegTypes.OBJECT_ID, param.getTimeFieldId())) {
            return XmResultJson.fail(5, "时间字段id格式错误");
        }
        if (!CollectionUtils.isEmpty(param.getTextFieldList())) {
            for (FieldParam fieldParam : param.getTextFieldList()) {
                if (!Pattern.matches(RegTypes.OBJECT_ID, fieldParam.getId())) {
                    return XmResultJson.fail(5, "训练字段id格式错误");
                }
            }
        }
        return null;
    }

    public CreateAnalysisProjectParam getCreateServiceParam(String userId, String orgId) {
        CreateAnalysisProjectParam param = new CreateAnalysisProjectParam();
        param.setUserId(userId);
        param.setOrgId(orgId);
        param.setName(this.name);
        param.setTableId(this.tableId);
        param.setTableName(this.tableName);
        List<TextFieldParam> textFieldParamList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(this.getTextFieldList())) {
            for (FieldParam fieldParam : this.textFieldList) {
                TextFieldParam textFieldParam = new TextFieldParam();
                textFieldParam.setId(fieldParam.getId());
                textFieldParam.setValue(fieldParam.getValue());
                textFieldParamList.add(textFieldParam);
            }
        }
        param.setModelId(this.modelId);
        param.setTextFields(textFieldParamList);
        param.setTimeFieldId(this.timeFieldId);
        param.setTimeFieldName(this.timeFieldName);
        return param;
    }


}
